﻿using Abp.Domain.Repositories;
using System;
using System.Collections.Generic;
using System.Text;
using MVlog.Core;
using System.Threading.Tasks;
using Abp.Application.Services.Dto;
using System.Linq;
using Microsoft.EntityFrameworkCore;
using Abp.Linq.Extensions;

namespace MVlog.Application
{
    public class HomePageAppService : AbpZeroTemplateAppServiceBase, IHomePageAppService
    {

        private readonly IRepository<Article, long> _articleRepository;
        private readonly IRepository<Tag, long> _tagRepository;
        private readonly IRepository<Picture, long> _pictureRepository;
        private readonly IRepository<Video, long> _videoRepository;
        private readonly IRepository<Comments, long> _commentsRepository;


        public HomePageAppService(
                    IRepository<Article, long> articleRepository,
                    IRepository<Tag, long> tagRepository,
                    IRepository<Picture, long> pictureRepository,
                    IRepository<Video, long> videoRepository,
                    IRepository<Comments, long> commentsRepository
            )
        {
            _articleRepository = articleRepository;
            _commentsRepository = commentsRepository;
            _pictureRepository = pictureRepository;
            _tagRepository = tagRepository;
            _videoRepository = videoRepository;
        }

        /// <summary>
        /// 根据用户ID查找用户
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<PagedResultDto<ArticleDto>> GetArticlesByUserId(GetHomePageInput input)
        {
            List<ArticleDto> returnList;
            if (input.UserId != 0)
            {
                var articles =await _articleRepository.GetAll().Where(o => o.Id == input.UserId).ToListAsync();
                returnList = ObjectMapper.Map<List<ArticleDto>>(articles);
            }
            else
            {
                returnList = new List<ArticleDto>();
            }
            var count = returnList.Count();
            var list = returnList.AsQueryable().PageBy(input).ToList();
            return new PagedResultDto<ArticleDto>(count,list);
        }
        /// <summary>
        /// 获取文章
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<PagedResultDto<ArticleDto>> GetArticles(GetHomePageAllArticlesInput input)
        {
            var data = await _articleRepository.GetAllListAsync();
            var count = data.Count();
            var list = ObjectMapper.Map<List<ArticleDto>>(data).AsQueryable().PageBy(input).ToList();
            return new PagedResultDto<ArticleDto>(count, list);
        }
        /// <summary>
        /// 获取标签
        /// </summary>
        /// <returns></returns>
        public async Task<ListResultDto<TagDto>> GetTags()
        {
            var data = await _tagRepository.GetAllListAsync();
            var list = ObjectMapper.Map<List<TagDto>>(data);
            return new ListResultDto<TagDto>(list);
        }

        /// <summary>
        /// 获取评论
        /// </summary>
        /// <param name="articleId"></param>
        /// <returns></returns>
        public async Task<ListResultDto<CommentsDto>> GetCommentsByArticleId( long articleId)
        {
            var data =(await _commentsRepository.GetAllListAsync()).Where(o => o.ArticleId == articleId);
            var list = ObjectMapper.Map<List<CommentsDto>>(data);
            return new ListResultDto<CommentsDto>(list);
        }
        /// <summary>
        /// 获取文章BY TAGID
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<PagedResultDto<ArticleDto>> GetArticleByTagId(GetHomePageArticleByTagIdInput input)
        {
            var data = (await _articleRepository.GetAllListAsync()).Where(o => o.TagIds.Contains(input.TagId.ToString()));
            var count = data.Count();
            var list = ObjectMapper.Map<List<ArticleDto>>(data).AsQueryable().PageBy(input).ToList();
            return new PagedResultDto<ArticleDto>(count, list);
        }
        /// <summary>
        /// 获取文章BYID
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public async Task<ArticleDto> GetArticleByID(long Id)
        {
            var odata =await _articleRepository.GetAsync(Id);
            var data=ObjectMapper.Map<ArticleDto>(odata);
            return data;
        }

        /// <summary>
        /// 获取图片
        /// </summary>
        /// <returns></returns>
        public async Task<ListResultDto<PictureDto>> GetPictures()
        {
            var odata =await _pictureRepository.GetAllListAsync();
            var list = ObjectMapper.Map<List<PictureDto>>(odata);
            return new ListResultDto<PictureDto>(list);
        }
        /// <summary>
        /// 获取视频
        /// </summary>
        /// <returns></returns>
        public async Task<ListResultDto<VideoDto>> GetVideos()
        {
            var odata = await _videoRepository.GetAllListAsync();
            var list = ObjectMapper.Map<List<VideoDto>>(odata);
            return new ListResultDto<VideoDto>(list);
        }

    }
}
